package com.isky.visual.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isky.visual.db.entity.AdsDatastore;
import com.isky.visual.db.entity.Source;
import com.isky.visual.db.factory.DbFactory;
import com.isky.visual.db.factory.IDbManager;
import com.isky.visual.db.mapper.AdsDatastoreMapper;
import com.isky.visual.db.service.IAdsDatastoreService;
import com.isky.visual.db.vo.BaseValues;
import com.isky.visual.db.vo.DefaultData;
import com.isky.visual.db.vo.DefaultValues;
import com.isky.visual.db.vo.ReportModel;
import com.isky.visual.result.CodeMsg;
import com.isky.visual.result.ResultVo;
import com.isky.visual.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 结果集 服务实现类
 * </p>
 *
 * @author wudl
 * @since 2020-04-19
 */
@Service("IAdsDatastoreService")
public class AdsDatastoreServiceImpl extends ServiceImpl<AdsDatastoreMapper, AdsDatastore> implements IAdsDatastoreService {

    @Autowired
    AdsDatastoreMapper datastoreMapper;

    @Override
    public List<AdsDatastore> list() {
        List<AdsDatastore> adsDatastores = baseMapper.selectList(null);
        return adsDatastores;
    }

    @Override
    public ResultVo<Object> getNameByReportModel(AdsDatastore adsDatastore) {


        QueryWrapper<AdsDatastore> queryWrappers = new QueryWrapper<>();
//        queryWrappers.eq("stat_name",adsDatastore.getStatName());
        queryWrappers.like("stat_name", adsDatastore.getStatName());
        List<Map<String, Object>> selectMaps = baseMapper.selectMaps(queryWrappers);
        List<String> dimension = new ArrayList<>();
        List<String> measure = new ArrayList<>();
        List<DefaultData> listdata = new ArrayList<>();
        List<BaseValues> listvalue = new ArrayList<>();
        List<String> listname = new ArrayList<>();
        DefaultData dt = null;
        DefaultValues vs = null;
        ReportModel rm = null;
        for (int i = 0; i < selectMaps.size(); i++) {
            dt = new DefaultData();
            vs = new DefaultValues();
            rm = new ReportModel();
            // 获取度量
            dt.setName((String) selectMaps.get(i).get("measure"));
            // 维度
//            dt.setValue((String) selectMaps.get(i).get("dimension"));
            listdata.add(dt);
            vs.setData(listdata);
            vs.setName((String) selectMaps.get(i).get("statName"));
            listvalue.add(vs);
            rm.setValues(listvalue);
            listname.add((String) selectMaps.get(i).get("dimension"));
            rm.setNames(listname);

        }

        return ResultVo.success(rm);
    }

    @Override
    public ResultVo<Object> getBarReport() {
        List<String> dimension = new ArrayList<>();
        List<String> measure = new ArrayList<>();
        List<DefaultData> listdata = null;
        List<BaseValues> listvalue = null;
        List<String> listname = null;
        List<Map<String, Object>> selectMaps = new ArrayList<Map<String, Object>>();

        DefaultData defaultData = null;
        DefaultValues vs = null;
        ReportModel rm = null;

        Source config = new Source();
        config.setUserName("root");
        config.setUserPwd("123456");
        config.setListenPort(3306);
        config.setDbUrl("10.204.125.109");
        config.setDbName("db_visual_wudl");
        config.setDbType("MySQL");
        try {
            IDbManager dbManager = new DbFactory(config).getDbManager();
            Connection connection = dbManager.getConnection();
//          String sql = "SELECT jobname,companyname,jobnum  FROM company ";
            String sql = "SELECT jobname,companyname,jobnum  FROM company  WHERE companyname = '百度科技'";
            listdata = new ArrayList<>();
            listname = new ArrayList<>();
            listvalue = new ArrayList<>();
            selectMaps = DBUtil.query(connection, sql);
            Map<Object, List<Map<String, Object>>> companynameSet = selectMaps.stream().collect(Collectors.groupingBy(k -> k.get("companyname")));
            Map<Object, List<Map<String, Object>>> jobnameMap = selectMaps.stream().collect(Collectors.groupingBy(k -> k.get("jobname")));
            Set<Object> entries = jobnameMap.keySet();
            for (Object jobName : entries) {
                listname.add(String.valueOf(jobName));
            }
            Set<Object> objects = companynameSet.keySet();
            for (Object keyname : objects) {

                System.out.println("keyname" + keyname);
                List<Map<String, Object>> maps = companynameSet.get(keyname);
                vs = new DefaultValues();
                List<DefaultData> someData = maps.stream().map(result -> {
                    DefaultData defaultData1 = new DefaultData();

                    defaultData1.setValue((Integer) result.get("jobnum"));
                    defaultData1.setName(String.valueOf(result.get("jobname")));
                    return defaultData1;
                }).collect(Collectors.toList());
                vs.setData(someData);
                rm = new ReportModel();


                vs.setName(String.valueOf(keyname));

                listvalue.add(vs);
                rm.setNames(listname);
                rm.setValues(listvalue);


            }


        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return ResultVo.success(CodeMsg.SUCCESS);
        }
        return ResultVo.success(rm);
    }

}
